<template>
  <div class="choose-question-root">
    <div class="input-container" @click="openDialog">
      <el-input
          v-model="(question || {}).itemName"
          placeholder="请选择"
          @click="openDialog"
          clearable
          :disabled="true"
          style="width: 240px"
      />
    </div>
    <el-dialog  ref="dialog" :title="width > 0 ? '选择隐患类别' : ''" :show-close="width > 0" :modal="width > 0" :visible.sync="open" :width="`${width}px`" append-to-body>
      <defect-type-list-page ref="defect" v-model="bufferQuestion"></defect-type-list-page>
      <div slot="footer" class="dialog-footer" v-if="width > 0">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import defectTypeListPage from '@/views/system/defectType/listPage.vue'

export default {
  components: {
    defectTypeListPage
  },
  props: {
    value: {
      type: [String, Number]
    }
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  watch: {
    value: {
      handler() {
        let {value, question} = this
        if (question == null) {
          let owner = this
          this.$nextTick(() => {
            owner.open = true;
            this.bufferQuestion = value
            setTimeout(() => {
              owner.open = false;
              owner.$nextTick(() => {
              })
            }, 1)
          })
        }
      },
      deep: true,
      immediate: true
    },
    bufferQuestion: {
      handler(val) {
        if (typeof val == 'object' && this.question == null) {
          this.submitForm()
        }
      },
      deep: true,
    }
  },
  data() {
    return {
      bufferQuestion: null,
      question: null,
      currentQuestionValue: [],
      open: false,
      width:0
    }
  },
  methods: {
    openDialog() {
      this.width = 1000
      this.open = true
    },
    submitForm() {
      let {bufferQuestion} = this
      this.question = _.cloneDeep(bufferQuestion)
      this.$emit('change', this.question?.id)
      this.open = false
    },
    cancel() {
      this.open = false
    },
  }
};
</script>

<style lang="scss" scoped>
.choose-question-root{
  width: 100%;
::v-deep .el-input__inner{
  background-color: #FFF;
  cursor: pointer;
}
}
</style>
